package com.foreknow.db;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.mchange.v2.c3p0.DataSources;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class c3p0Utils {
    private static ComboPooledDataSource ds = new ComboPooledDataSource();
    //自动加载c3p0-config.xml文件
    public static DataSource getDataSourse(){
        return ds;
    }
    public static Connection getConnection() throws SQLException{
        //获取连接，从c3p0连接池获取
        return ds.getConnection();
    }
    //关闭所有资源的统一代码
    public static void closeAll(Connection conn, Statement st, ResultSet rs){
        //负责关闭
        if(conn != null){
            try{
                conn.close();
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
        if(st != null){
            try{
                st.close();
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
        if (rs != null){
            try{
                rs.close();
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
    }
}
